﻿using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
using FantasyDraft2.Models;

namespace FantasyDraft2.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /User/
        FantasyDraftEntities _fantasyDb = new FantasyDraftEntities();
        
        public ActionResult Index()
        {
            var users = _fantasyDb.Users.ToList();
            return View(users);
        }

        //
        // GET: /User/Details/5

        public ActionResult Details(int id)
        {
            UserDetailsViewModel model = new UserDetailsViewModel
            {
                comments = _fantasyDb.UserComments.Where(x => x.UserId == id),
                players = _fantasyDb.Players.Where(x => x.TeamId == id),
                userId = id
            };
            
            return View(model);
        }

        [HttpPost]
        public ActionResult Details(UserDetailsViewModel model, string button)
        {
            switch (button){
                case "Add Comment":
                    AddUserCommentViewModel temp = new AddUserCommentViewModel
                    {
                        userId = model.userId
                    };

                    TempData["uc"] = temp;
                    return RedirectToAction("AddComment");
                case "Return to Team Overview":
                    return RedirectToAction("Index");
            }

            return RedirectToAction("Index");
        }

        public ActionResult AddComment()
        {
            AddUserCommentViewModel model = (AddUserCommentViewModel)TempData["uc"];
            return View(model);
        }

        [HttpPost]
        public ActionResult AddComment(AddUserCommentViewModel model, string button)
        {
            switch (button)
            {
                case "Cancel":
                    return RedirectToAction("Details", new { id = model.userId });
                case "Add Comment":
                    UserComment c = new UserComment
                    {
                        Comment = model.comment,
                        UserId = model.userId,
                        User = _fantasyDb.Users.SingleOrDefault(x => x.UserId == model.userId)
                    };

                    _fantasyDb.UserComments.Add(c);
                    _fantasyDb.SaveChanges();
                    return RedirectToAction("Details", new { id = model.userId });
            }

            return RedirectToAction("Index");
        }

        public ActionResult Bench(int pid, int tid)
        {
            // Only one benched player per team, set all players to active

            var players = _fantasyDb.Players.Where(p => p.TeamId == tid).ToList();
            players.ForEach(p => p.IsActive = true);
            _fantasyDb.SaveChanges();

            // Set IsActive to false for player with supplied pid
            var player = _fantasyDb.Players.Find(pid);
            player.IsActive = false;
            _fantasyDb.SaveChanges();

            return RedirectToAction("Details", "User", new {id = tid});
        }

        public ActionResult Active(int pid, int tid)
        {
            var player = _fantasyDb.Players.Find(pid);
            var activePlayers = _fantasyDb.Players.Where(x => x.TeamId == tid).Where(y => y.IsActive==true);
            if (activePlayers.Count() == 4)
            {

                //Display message and don't let them
                ModelState.AddModelError("PlayerName","You can only activate 4 players.");
                return RedirectToAction("Details", "User", new { id = tid });
            }
            else
            {
                player.IsActive = true;
            }
            
            _fantasyDb.SaveChanges();

            return RedirectToAction("Details", "User", new { id = tid }); 
        }
        
         //GET: /User/Register

        public ActionResult Register()
        {
            return View();
        }

        //
        // POST: /User/Register

        [HttpPost]
        public ActionResult Register(User user)
        {
            if (ModelState.IsValid)
            {
                _fantasyDb.Users.Add(user);
                _fantasyDb.SaveChanges();

                string name = user.FirstName;

                Session.Add("User", name);
                
                return RedirectToAction("Index");
            }
            return View(user);
        }

        //
        // GET: /User/Logon

        public ActionResult Logon()
        {
            return View();
        }

        //
        // POST: /User/Logon
        [HttpPost]
        public ActionResult Logon(User user)
        {

            FormsAuthentication.SetAuthCookie(user.UserName, true);
            return RedirectToAction("Index", "User");
        }

        // GET: /Account/Logoff
        public ActionResult Logoff()
        {
            return RedirectToAction("Index", "Home");
        }

    }
}
